﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NormalAttack : MonoBehaviour
{

    public float BaseAttackInternal;
    //基础速率系数

    public bool canAttack;

    private BaseStatusInfo MyInfo;
#region 委托及相关参数
    //攻击回调
    private Action<int> AttackAction;
    //冷却计时
    private Action CoolDownAction;
    private float ActionBaseTime;
    private ParticleSystem NormalAttackEffect;
#endregion

    public void Init()
    {
        MyInfo = GetComponent<BaseStatusInfo>();
        ActionBaseTime = MyInfo.ActionBaseTime;
        NormalAttackEffect = transform.Find("普通物理攻击").GetComponent<ParticleSystem>();
    }

    private void Update()
    {
        if (CoolDownAction != null)
        {
            CoolDownAction();
        }
    }

    /// <summary>
    /// 点击屏幕按钮，普通攻击
    /// </summary>
    public void ClickNormalAttackBtn()
    {
        //检测cd,还在cd点击无效
        if(!canAttack) return;
        //可以攻击，生成粒子,执行动画,发送消息给服务器，伤害逻辑判定在服务端处理
        NormalAttackEffect.Play();

        //执行攻击回调
        if (AttackAction != null)
            AttackAction(MyInfo.normalAttackDamage);
        //开始普攻冷却
        CoolDownAction = CoolDownCounter;
        canAttack = false;
    }

    /// <summary>
    /// CD
    /// </summary>
    public void CoolDownCounter()
    {
        ActionBaseTime -= (Time.deltaTime*MyInfo.ActionSpeedRatio);
        if (ActionBaseTime <= 0)
        {
            //cd时间到了，点击普攻按钮可以攻击
            canAttack = true;
            ActionBaseTime = MyInfo.ActionBaseTime;
            CoolDownAction = null;
        }
    }
}
